#include "inviteexpert.h"
#include "ui_inviteexpert.h"
#include <QPushButton>
#include <QDebug>
#include "../main/mainwindow.h"
#include <QStyle>
#include <QScreen>
#include <QApplication>
#include "User.h"
#include "Invitation.h"
#include "../../room/RoomHandler.h"

extern User user;

InviteExpert::InviteExpert(RoomHandler* roomHandler, QWidget *parent) :
    QWidget(parent), roomHandler(roomHandler),
    ui(new Ui::InviteExpert)
{
    ui->setupUi(this);

    if(user.userType == 1){
        ui->tableWidget->setColumnCount(2);
        ui->tableWidget->setHorizontalHeaderLabels({"邀请者用户名", "加入"});
        ui->tableWidget->horizontalHeader()->setStretchLastSection(true);
        ui->tableWidget->setColumnWidth(1, 45);
        ui->tableWidget->setColumnWidth(0, 210);

    }
    else{
    ui->tableWidget->setColumnCount(2);
    ui->tableWidget->setHorizontalHeaderLabels({"专家的用户名", "邀请"});
    ui->tableWidget->horizontalHeader()->setStretchLastSection(true);
    ui->tableWidget->setColumnWidth(1, 45);
    ui->tableWidget->setColumnWidth(0, 210);
    }

    invitationHandler = new InvitationHandler(roomHandler, this);
}

InviteExpert::~InviteExpert()
{
    delete ui;
}

//往tablewidget里加专家用户名和按钮，ticket是用户名，直接调用这个函数
void InviteExpert::addRow(const quint16& id, const QString &ticket)
{
    int row = ui->tableWidget->rowCount();
    ui->tableWidget->insertRow(row);

    ui->tableWidget->setItem(row, 0, new QTableWidgetItem(ticket));

    if(user.userType == 1){
        QPushButton *btn = new QPushButton("加入", this);
        btn->setFixedSize(45, 45);
        ui->tableWidget->setCellWidget(row, 1, btn);
        connect(btn, &QPushButton::clicked, this, [this, id] { onJoinBtnClicked(id); });
    }
    else{
        QPushButton *btn = new QPushButton("邀请", this);
        btn->setFixedSize(45, 45);
        ui->tableWidget->setCellWidget(row, 1, btn);
        connect(btn, &QPushButton::clicked, this, [this, id] { onInviteBtnClicked(id); });
    }
}

//点击邀请按钮
void InviteExpert::onInviteBtnClicked(const quint16& userId)
{
    invitationHandler->sendInvitation(roomId, userId);
}

void InviteExpert::onJoinBtnClicked(const quint16& roomId)
{
    roomHandler->joinRoom(roomId);
    this->hide();
}

//每次显示时居中
void InviteExpert::showEvent(QShowEvent *event)
{
    move(
        QStyle::alignedRect(
            Qt::LeftToRight,
            Qt::AlignCenter,
            size(),
            qApp->primaryScreen()->availableGeometry()
        ).topLeft()
    );

    QWidget::showEvent(event);
}

void InviteExpert::recvExperts(const QByteArray& data)
{
    ui->tableWidget->setRowCount(0); // 清空旧数据

    QList<User> experts;
    QByteArrayList records = data.split(';');
    for (const auto& records : records) {
        QByteArrayList fields = records.split(',');
        if (fields.size() < 3) continue;
        User expert(fields[0].toUInt(), fields[1], fields[2].toUShort());
        experts.append(expert);
    }
    for (const auto& expert : experts) {
        addRow(expert.id, expert.username);
    }
}

void InviteExpert::recvInvitation(const QByteArray& data)
{
    ui->tableWidget->setRowCount(0); // 清空旧数据

    QByteArrayList records = data.split(';');
    for (const auto& record : records) {
        QByteArrayList fields = record.split(',');
        if (fields.size() < 3) continue;
        addRow(fields[1].toUShort(), "邀请加入会议");
    }
}